package com.dq.controller.user.centre;

import com.dq.controller.in.user.UserSchedulingQuery;
import com.dq.controller.in.user.UserSummaryQuery;
import com.dq.domain.sys.user.user.centre.UserScheduling;
import com.dq.dto.DayDto;
import com.dq.exception.customException.AdminException;
import com.dq.service.AllService;
import com.dq.utils.TimeUtil;
import com.dq.vo.Vo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author luchangtong
 * @Date 2020/5/9 18:01
 */
@RestController
@RequestMapping("/sys/userScheduling/")
@Api(description = "员工排班")
public class SysUserSchedulingController extends AllService {

    @ApiOperation("列表")
    @PostMapping("list")
    public Vo list(@RequestBody UserSummaryQuery userSummaryQuery) throws AdminException{

        return new Vo(userSchedulingService.list(userSummaryQuery.getUserId(), userSummaryQuery.getYear(), userSummaryQuery.getMonth(),null));
    }

    @ApiOperation("设置状态")
    @PostMapping("set/{id}")
    public Vo set(@PathVariable Long id) throws AdminException{

        return userSchedulingService.set(id);
    }

    @ApiOperation("保存")
    @PostMapping("save")
    public Vo save(@RequestBody  List<UserScheduling> userSchedulings) throws AdminException{

        return userSchedulingService.save(userSchedulings);
    }

    @ApiOperation("根据年月获取当月的日期")
    @PostMapping("findDayByYearAndMonth")
    public Vo findDayByYearAndMonth(@RequestBody UserSchedulingQuery userSchedulingQuery) throws AdminException{
        List<String> strings = TimeUtil.getDayByMonth(userSchedulingQuery.getYear().intValue(),userSchedulingQuery.getMonth().intValue());
        List<DayDto> list = new ArrayList<>();
        strings.forEach(s -> {
            DayDto dayDto = new DayDto();
            dayDto.setDate(s);
            String[] a = s.split("-");
            UserScheduling.Vo userScheduling = userSchedulingService.listByDay(userSchedulingQuery.getUserId(), userSchedulingQuery.getYear(), userSchedulingQuery.getMonth(),new Long(a[2]));
            dayDto.setId(userScheduling.getId());
            dayDto.setUserId(userSchedulingQuery.getUserId());
            dayDto.setStartTime(userScheduling.getStartTime());
            dayDto.setEndTime(userScheduling.getEndTime());
            dayDto.setWorkStatusCn(userScheduling.getWorkStatusCn());
            dayDto.setWorkStatus(userScheduling.getWorkStatusEn());
            dayDto.setYear(new Long(a[0]));
            dayDto.setMonth(new Long(a[1]));
            dayDto.setDay(new Long(a[2]));
            list.add(dayDto);
        });
        return new Vo(list);
    }
}
